package com.hudson.plugin.ccheck.internal.domain.dao;

import com.hudson.plugin.ccheck.domain.dao.CcheckresultDAOInterface;
import com.hudson.plugin.ccheck.internal.domain.entity.CcheckresultEntity;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * Ccheckresult entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .Ccheckresult
 */

public class CcheckresultDAO extends HibernateDaoSupport implements CcheckresultDAOInterface {
    private static final Logger log = LoggerFactory
            .getLogger(CcheckresultDAO.class);

    protected void initDao() {
        // do nothing
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void save(CcheckresultEntity transientInstance) {
        log.debug("saving Ccheckresult instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delete(CcheckresultEntity persistentInstance) {
        log.debug("deleting Ccheckresult instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public CcheckresultEntity findById(Integer id) {
        log.debug("getting Ccheckresult instance with id: " + id);
        try {
            CcheckresultEntity instance = (CcheckresultEntity) getHibernateTemplate().load(
                    CcheckresultEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<CcheckresultEntity> findByExample(CcheckresultEntity instance) {
        log.debug("finding Ccheckresult instance by example");
        try {
            List<CcheckresultEntity> results = (List<CcheckresultEntity>) getHibernateTemplate()
                    .findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Ccheckresult instance with property: "
                + propertyName + ", value: " + value);
        try {
            String queryString = "from CcheckresultEntity as model where model."
                    + propertyName + " = ?";
            return getHibernateTemplate().find(queryString, value);
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<CcheckresultEntity> findByBuildId(Object buildId) {
        return findByProperty(BUILD_ID, buildId);
    }

    public List<CcheckresultEntity> findByScore(Object score) {
        return findByProperty(SCORE, score);
    }

    public List<CcheckresultEntity> findBySummary(Object summary) {
        return findByProperty(SUMMARY, summary);
    }

    public List<CcheckresultEntity> findByResultReason(Object resultReason) {
        return findByProperty(RESULT_REASON, resultReason);
    }

    public List<CcheckresultEntity> findByDetails(Object details) {
        return findByProperty(DETAILS, details);
    }

    public List<CcheckresultEntity> findByDisplayName(Object displayName) {
        return findByProperty(DISPLAY_NAME, displayName);
    }

    public List findAll() {
        log.debug("finding all Ccheckresult instances");
        try {
            String queryString = "from CcheckresultEntity";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public CcheckresultEntity merge(CcheckresultEntity detachedInstance) {
        log.debug("merging Ccheckresult instance");
        try {
            CcheckresultEntity result = (CcheckresultEntity) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void attachDirty(CcheckresultEntity instance) {
        log.debug("attaching dirty Ccheckresult instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void attachClean(CcheckresultEntity instance) {
        log.debug("attaching clean Ccheckresult instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static CcheckresultDAOInterface getFromApplicationContext(
            ApplicationContext ctx) {
        return (CcheckresultDAOInterface) ctx.getBean("CcheckresultDAO");
    }


    public List<Integer> findCCheckResultIdByBuildsId(Integer buildId) {
        log.debug("finding CCheck ResultId By BuildsId");
        try {
            Query query = this.getSession().createQuery("select c.id from CcheckresultEntity c where c.buildid = :buildId");
            query.setParameter("buildId", buildId);
            List<Integer> results = query.list();
            return results;
        } catch (RuntimeException re) {
            log.error("find CCheck ResultId By BuildsId failed", re);
            throw re;
        }
    }
}